var Cur_Url_ThreadCM = window.location.href,
	Cm_Block = document.getElementById('cm_block'),
	Cm_Block_Content = Cm_Block.innerHTML,
	Cm_Item_Id = [],
	Cm_Item_Content = [],
	Cm_Item_Level = [],
	Cm_Num = 0,
	Cm_Item_Id_T = '',
	Cm_Item_Content_T = '',
	Cm_Item_Level_T = '',
	i = 0,
	j = 0,
	k = 0,
	h = 0,
	strout = '',
	str_t1 = "",
	str_t2 = "";

function getInternetExplorerVersion() {
	var rv = -1;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re = new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})");
		if (re.exec(ua) !== null) rv = parseFloat(RegExp.$1);
	}
	return rv;
}
var IE_ver = getInternetExplorerVersion();
if (IE_ver == -1 || IE_ver >= 9) {
	while (Cm_Block_Content.indexOf('id="c') != -1) {
		i = Cm_Block_Content.indexOf('id="c');
		Cm_Block_Content = Cm_Block_Content.substring(i + 4);
		i = Cm_Block_Content.indexOf('"');
		Cm_Item_Id[Cm_Num] = Cm_Block_Content.substring(0, i);
		Cm_Block_Content = Cm_Block_Content.substring(i);
		Cm_Item_Content[Cm_Num] = document.getElementById(Cm_Item_Id[Cm_Num]).innerHTML;
		Cm_Item_Level[Cm_Num] = 0;
		Cm_Num++;
	}
	for (i = 0; i < Cm_Num - 1; i++) {
		for (j = i + 1; j < Cm_Num; j++) {
			if (Cm_Item_Content[j].indexOf(Cm_Item_Id[i]) != -1) {
				Cm_Item_Id_T = Cm_Item_Id[j];
				Cm_Item_Content_T = Cm_Item_Content[j];
				Cm_Item_Level[j] = Cm_Item_Level[i] + 1;
				Cm_Item_Level_T = Cm_Item_Level[j];
				for (h = i + 1; h < j; h++) {
					if (Cm_Item_Level[h] < Cm_Item_Level_T) {
						break;
					}
				}
				for (k = j; k > h; k = k - 1) {
					Cm_Item_Id[k] = Cm_Item_Id[k - 1];
					Cm_Item_Content[k] = Cm_Item_Content[k - 1];
					Cm_Item_Level[k] = Cm_Item_Level[k - 1];
				}
				Cm_Item_Id[h] = Cm_Item_Id_T;
				Cm_Item_Content[h] = Cm_Item_Content_T;
				Cm_Item_Level[h] = Cm_Item_Level_T;
			}
		}
	}
	for (i = 0; i < Cm_Num; i++) {
		j = Cm_Item_Content[i].indexOf('@<a href="#c');
		if (j != -1) {
			str_t1 = Cm_Item_Content[i].substring(0, j);
			str_t2 = Cm_Item_Content[i].substring(j + 1);
			j = str_t2.indexOf('</a>');
			str_t2 = str_t2.substring(j + 4);
			Cm_Item_Content[i] = str_t1 + str_t2;
		}
		j = Cm_Item_Content[i].indexOf('class="cm_wrap ');
		if (j != -1) {
			str_t1 = Cm_Item_Content[i].substring(0, j);
			str_t2 = Cm_Item_Content[i].substring(j);
			if (Cm_Item_Level[i] > 5) Cm_Item_Level[i] = 5;
			Cm_Item_Content[i] = str_t1 + 'style="width:' + (100 - Cm_Item_Level[i] * 5) + '%" ' + str_t2;
		}
		strout += Cm_Item_Content[i];
	}
	strout += '<div class="clear"></div>';
	Cm_Block.innerHTML = strout;
	Cm_Block.style.display = 'block';
	var Cm_Total_Obj = document.getElementById('cm_total').innerHTML;
	var Cm_Total = parseInt(Cm_Total_Obj, 10);
	if (Cm_Total > 200) {
		strout = '<div style="float:left">Page ';
		var Cm_Page_Num = (Cm_Total - Cm_Total % 200) / 200 + 1;
		var Org_Url_ThreadCM = '';
		var Cm_Cur_Page = 1;
		i = Cur_Url_ThreadCM.indexOf('.html');
		if (i != -1) {
			Org_Url_ThreadCM = Cur_Url_ThreadCM.substring(0, i + 5);
		} else {
			Org_Url_ThreadCM = Cur_Url_ThreadCM;
		}
		i = Org_Url_ThreadCM.indexOf('#comments');
		if (i != -1) {
			Org_Url_ThreadCM = Org_Url_ThreadCM.substring(0, i);
		}
		i = Cur_Url_ThreadCM.indexOf('?commentPage=');
		if (i == -1) {
			Cm_Cur_Page = 1;
		} else {
			Cm_Cur_Page = parseInt(Cur_Url_ThreadCM.substring(i + 13), 10);
		}
		for (i = 1; i <= Cm_Page_Num; i++) {
			if (i == Cm_Cur_Page) {
				strout += '<span>' + i + '</span>';
			} else {
				strout += '<a href="' + Org_Url_ThreadCM + '?commentPage=' + i + '#comments">' + i + '</a>';
			}
		}
		if (Cm_Cur_Page * 200 <= Cm_Total) {
			strout += '</div><div style="float:right">' + (((Cm_Cur_Page - 1) * 200) + 1) + ' - ' + (Cm_Cur_Page * 200) + ' of ' + Cm_Total + ' comments</div>';
		} else {
			strout += '</div><div style="float:right">' + (((Cm_Cur_Page - 1) * 200) + 1) + ' - ' + Cm_Total + ' of ' + Cm_Total + ' comments</div>';
		}
		var Cm_Page_Obj = document.getElementById('cm_page');
		Cm_Page_Obj.innerHTML = strout;
		Cm_Page_Obj = document.getElementById('cm_page_copy');
		Cm_Page_Obj.innerHTML = strout;
		if (Cm_Cur_Page < Cm_Page_Num) {
			strout = '<style type="text/css">.cm_author_reply {display: none}</style>';
			var Cm_ReplyCSS_Obj = document.getElementById('cm_reply_css');
			Cm_ReplyCSS_Obj.innerHTML = strout;
		}
	}
} else {
	while (Cm_Block_Content.indexOf('id=c') != -1) {
		i = Cm_Block_Content.indexOf('id=c');
		Cm_Block_Content = Cm_Block_Content.substring(i + 3);
		i = Cm_Block_Content.indexOf('>');
		Cm_Item_Id[Cm_Num] = Cm_Block_Content.substring(0, i);
		Cm_Block_Content = Cm_Block_Content.substring(i);
		Cm_Item_Content[Cm_Num] = document.getElementById(Cm_Item_Id[Cm_Num]).innerHTML;
		Cm_Item_Level[Cm_Num] = 0;
		Cm_Num++;
	}
	for (i = 0; i < Cm_Num - 1; i++) {
		for (j = i + 1; j < Cm_Num; j++) {
			if (Cm_Item_Content[j].indexOf(Cm_Item_Id[i]) != -1) {
				Cm_Item_Id_T = Cm_Item_Id[j];
				Cm_Item_Content_T = Cm_Item_Content[j];
				Cm_Item_Level[j] = Cm_Item_Level[i] + 1;
				Cm_Item_Level_T = Cm_Item_Level[j];
				for (h = i + 1; h < j; h++) {
					if (Cm_Item_Level[h] < Cm_Item_Level_T) {
						break;
					}
				}
				for (k = j; k > h; k = k - 1) {
					Cm_Item_Id[k] = Cm_Item_Id[k - 1];
					Cm_Item_Content[k] = Cm_Item_Content[k - 1];
					Cm_Item_Level[k] = Cm_Item_Level[k - 1];
				}
				Cm_Item_Id[h] = Cm_Item_Id_T;
				Cm_Item_Content[h] = Cm_Item_Content_T;
				Cm_Item_Level[h] = Cm_Item_Level_T;
			}
		}
	}
	for (i = 0; i < Cm_Num; i++) {
		j = Cm_Item_Content[i].indexOf('@<A href="#c');
		if (j != -1) {
			str_t1 = Cm_Item_Content[i].substring(0, j);
			str_t2 = Cm_Item_Content[i].substring(j + 1);
			j = str_t2.indexOf('</A>');
			str_t2 = str_t2.substring(j + 4);
			Cm_Item_Content[i] = str_t1 + str_t2;
		}
		j = Cm_Item_Content[i].indexOf('class="cm_wrap');
		if (j != -1) {
			str_t1 = Cm_Item_Content[i].substring(0, j);
			str_t2 = Cm_Item_Content[i].substring(j);
			if (Cm_Item_Level[i] > 5) Cm_Item_Level[i] = 5;
			Cm_Item_Content[i] = str_t1 + 'style="width:' + (100 - Cm_Item_Level[i] * 5) + '%" ' + str_t2;
		}
		strout += Cm_Item_Content[i];
	}
	strout += '<div class="clear"></div>';
	Cm_Block.innerHTML = strout;
	Cm_Block.style.display = 'block';
	var Cm_Total_Obj = document.getElementById('cm_total').innerHTML;
	var Cm_Total = parseInt(Cm_Total_Obj, 10);
	if (Cm_Total > 200) {
		strout = '<div style="float:left">Page ';
		var Cm_Page_Num = (Cm_Total - Cm_Total % 200) / 200 + 1;
		var Org_Url_ThreadCM = '';
		var Cm_Cur_Page = 1;
		i = Cur_Url_ThreadCM.indexOf('.html');
		if (i != -1) {
			Org_Url_ThreadCM = Cur_Url_ThreadCM.substring(0, i + 5);
		} else {
			Org_Url_ThreadCM = Cur_Url_ThreadCM;
		}
		i = Org_Url_ThreadCM.indexOf('#comments');
		if (i != -1) {
			Org_Url_ThreadCM = Org_Url_ThreadCM.substring(0, i);
		}
		i = Cur_Url_ThreadCM.indexOf('?commentPage=');
		if (i == -1) {
			Cm_Cur_Page = 1;
		} else {
			Cm_Cur_Page = parseInt(Cur_Url_ThreadCM.substring(i + 13), 10);
		}
		for (i = 1; i <= Cm_Page_Num; i++) {
			if (i == Cm_Cur_Page) {
				strout += '<span>' + i + '</span>';
			} else {
				strout += '<a href="' + Org_Url_ThreadCM + '?commentPage=' + i + '#comments">' + i + '</a>';
			}
		}
		if (Cm_Cur_Page * 200 <= Cm_Total) {
			strout += '</div><div style="float:right">' + (((Cm_Cur_Page - 1) * 200) + 1) + ' - ' + (Cm_Cur_Page * 200) + ' of ' + Cm_Total + ' comments</div>';
		} else {
			strout += '</div><div style="float:right">' + (((Cm_Cur_Page - 1) * 200) + 1) + ' - ' + Cm_Total + ' of ' + Cm_Total + ' comments</div>';
		}
		var Cm_Page_Obj = document.getElementById('cm_page');
		Cm_Page_Obj.innerHTML = strout;
		Cm_Page_Obj = document.getElementById('cm_page_copy');
		Cm_Page_Obj.innerHTML = strout;
		if (Cm_Cur_Page < Cm_Page_Num) {
			strout = '<style type="text/css">.cm_author_reply {display: none}</style>';
			var Cm_ReplyCSS_Obj = document.getElementById('cm_reply_css');
			Cm_ReplyCSS_Obj.innerHTML = strout;
		}
	}
}
/* JQUERY */
$(function() {
	// Memperbaiki masa lalu
	$('.cm_name a:contains(Post Man)').html('Leo da Costa').parents('.cm_entry').prepend('<span class="author-mark"></span>');
	// GLOBAL VARIABLES
	var $mypage = $('html, body'),
		$ansbutton = $('a.answer-this-comment'),
		$form = $('#comment-form'),
		$commentform = $('#comment-editor'),
		source = $commentform.attr('src').split('#');

	function answerCm() {
		// $('#alert-box').hide();
		$commentform.css('opacity', 0);
		$('#loadingtext').html('<span>Loading...</span>');
		$ansbutton.removeClass('clicked');
		$(this).addClass('clicked');
		var start = source[0],
			end = source[1],
			parentID = $(this).data('rel').substr(1),
			replyFrame = start + '&parentID=' + parentID + '#' + end,
			$goto = $(this).parents('.cm_wrap');
		$form.appendTo($goto);
		$mypage.scrollTop($goto.offset().top - 30);
		$commentform.attr('src', '').one("load", function() {
			$('#loadingtext').html('<span>Loading...</span>');
			$(this).attr('src', replyFrame).on("load", function() {
				$(this).css('opacity', 1);
				$('#loadingtext span').remove();
			});
		});
		return false;
	}

	$ansbutton.on("click", answerCm);


	$('#cm_block').on("click", "#cancel-answer", function() {
		var origformsrc = $('#comment-editor-src').attr('href'),
			$ground = $('#comments');
		$('#loadingtext').html('<span>Reloading iframe...</span>');
		$form.appendTo($ground);
		$commentform.css('opacity', 0).attr('src', origformsrc + '#' + source[1]).on("load", function() {
			$('#loadingtext span').remove();
			$(this).css('opacity', 1);
		});
		$ansbutton.removeClass('clicked');
		return false;
	});

});